import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import static java.lang.Thread.sleep;

public class Main {
    static WebDriver webDriver;
    public static void main(String[] args) throws InterruptedException {
//        System.out.println(testLogin());
//        System.out.println(testMatch());
        System.out.println(testRegister());

    }
    static String testMatch() throws InterruptedException {
        //定位匹配按钮
        WebElement button = webDriver.findElement(By.id("match"));
        String url = webDriver.getCurrentUrl();
        button.click();

        //等待匹配成功
        sleep(3000);
        //判断释放页面跳转
        if(!url.equals(webDriver.getCurrentUrl())) {
            return "testMatch: true";
        }
        return "testMatch: false";
    }
    static String testLogin() throws InterruptedException {
        //启动chrome浏览器
        webDriver = new ChromeDriver();

        //打开百度
        webDriver.get("http://127.0.0.1:8080/login.html");

        //定位到输入框
        WebElement input = webDriver.findElement(By.id("username"));
        input.sendKeys("Ting");

        input = webDriver.findElement(By.id("password"));
        input.sendKeys("123456");

        String url = webDriver.getCurrentUrl();
        //定位搜索按钮
        WebElement button = webDriver.findElement(By.className("submit"));
        button.click();


        // 等待新窗口打开
        sleep(1000);

        if(!url.equals(webDriver.getCurrentUrl())) {
            return "testLogin: true";
        }
        return "testLogin: false";
    }
    static String testRegister() throws InterruptedException {
        //启动chrome浏览器
        webDriver = new ChromeDriver();

        //打开百度
        webDriver.get("http://127.0.0.1:8080/register.html");

        //定位到输入框
        WebElement input = webDriver.findElement(By.id("username"));
        input.sendKeys("Ping");

        input = webDriver.findElement(By.id("password"));
        input.sendKeys("123456");

        String url = webDriver.getCurrentUrl();
        //定位搜索按钮
        WebElement button = webDriver.findElement(By.className("submit"));
        button.click();

        // 等待
        sleep(1000);

        //切换到弹窗点击确定
        webDriver.switchTo().alert().accept();

        // 等待新窗口打开
        sleep(1000);
        if(!url.equals(webDriver.getCurrentUrl())) {
            return "testRegister: true";
        }
        return "testRegister: false";
    }
}
